﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace WebServiceRequesterTraduction
{
    public partial class WebFormServiceRequesterTraduction : System.Web.UI.Page
    {
        private wsRefTraduction.WebServiceTraduction wsrefTraduction;
        private wsRefCalculator.WebServiceCalculator wsrefCalculator;

        protected void Page_Load(object sender, EventArgs e)
        {
            wsrefTraduction = new wsRefTraduction.WebServiceTraduction();
            wsrefCalculator = new wsRefCalculator.WebServiceCalculator();
        }

        protected void ButtonTraduire_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBoxLien.Text.Trim() != string.Empty)
                {
                    string result = wsrefTraduction.FormaterRequete(TextBoxLien.Text, getAccronyme(DropDownListDepart.Text), getAccronyme(DropDownListArrive.Text));
                    if (result == "-1")
                    {
                        Response.Write("<script>alert('Error for traduction')</script>");
                    }
                    else
                    {
                        RunUrl(result, true);
                        string source = "";
                        // Converti la chaîne en tableau de byte
                        byte[] tab = Encoding.Default.GetBytes(result);
                        foreach (byte elt in tab)
                        {
                            source = source + elt.ToString();
                        }

                        //Utilisation du webservice 3
                        result = wsrefCalculator.HexToBin(source);

                        Response.Write("<script>alert('Traduction en binaire de la chaine de bytes du lien convertit : " + result + "')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('You must give to me a valid URL')</script>");
                }
            }
            catch
            {
                Response.Write("<script>alert('Error for traduction')</script>");
            }
        }

        [System.Runtime.InteropServices.DllImportAttribute("shell32.dll", SetLastError = true)]
        static extern int ShellExecute(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
        //
        public void RunUrl(string sUrl, bool bMaximized)
        {
            ShellExecute(0, "open", sUrl, string.Empty, string.Empty, (bMaximized == true) ? 3 : 1);
        }

        private String getAccronyme(string langue)
        {
            switch (langue)
            {
                case "Allemand":
                    return "de";
                case "Anglais":
                    return "en";
                case "Chinois":
                    return "zt";
                case "Français":
                    return "fr";
                case "Grec":
                    return "el";
                case "Italien":
                    return "it";
                case "Japonais":
                    return "ja";
                case "Neerlandais":
                    return "nl";
                case "Portuguais":
                    return "pt";
                case "Russe":
                    return "ru";
                default:
                    return "-1";
            }
        }
    }
}